package com.chanyachting.emall.action.businesscategory;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Businesscategory;
import com.chanyachting.emall.domain.Businesstype;
import com.chanyachting.emall.form.BusinesscategoryFrom;
import com.chanyachting.emall.service.BusinesscategoryService;
import com.chanyachting.emall.service.BusinesstypeService;
import com.chanyachting.emall.serviceImpl.BusinesscategoryServiceImpl;
import com.chanyachting.emall.serviceImpl.BusinesstypeServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class BusinessCategoryAction extends DispatchAction {
	
	
	
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		BusinesstypeService bcs=new BusinesstypeServiceImpl();
		
		Businesstype btype=new Businesstype();
		
		btype.setId(id);
		
		//根据行业分类查询所有行业次分类
		List list=bcs.getBusinesscategoryByType(btype);
		
		new RequestUtil(request).setAttribute("businesscategoryList", list);
		
		return mapping.findForward("list");
	}
	

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		List list=bcs.getAllBusinesscategory();
		
		new RequestUtil(request).setAttribute("businesscategoryList", list);
	
		return mapping.findForward("list");
	}
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		Businesscategory bcategory=new Businesscategory();
		
		bcategory.setId(id);
		
		if(request.getMethod().equals("POST")){
			
			BusinesscategoryFrom bcform=(BusinesscategoryFrom)form;
			
			//如果验证成功
			if(bcform.validate(mapping, request).isEmpty()){
				
				BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
				
				Businesscategory bc=new Businesscategory();
				
				BeanUtils.copyProperties(bc, bcform);
				
				bcs.updateBusinesscategory(bc);
				
				new RequestUtil(request).setSuccessMsg();
				
			}
			
		}

		//重新加载行业次分类对象
		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		bcategory=bcs.getOneBusinesscategory(bcategory);
	
		new RequestUtil(request).setAttribute("businesscategory", bcategory);
		
		//加载行业类型
		BusinesstypeService bs=new BusinesstypeServiceImpl();
		
		List list=bs.getAllBusinesstype();
		
		new RequestUtil(request).setAttribute("businesstypeList", list);
		
		return mapping.findForward("update");
	}
	
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//如果请求方式为 post
		if(request.getMethod().equals("POST")){
			
			BusinesscategoryFrom bcform=(BusinesscategoryFrom)form;
			
			//如果验证成功
			if(bcform.validate(mapping, request).isEmpty()){
				
				BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
				
				Businesscategory bc=new Businesscategory();
				
				BeanUtils.copyProperties(bc, bcform);
				
				bcs.addBusinesscategory(bc);
				
				new RequestUtil(request).setSuccessMsg();
				
			}

		}
		
		BusinesstypeService bs=new BusinesstypeServiceImpl();
		
		List list=bs.getAllBusinesstype();
		
		new RequestUtil(request).setAttribute("businesstypeList", list);
		
		return mapping.findForward("create");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		int byId=Integer.parseInt(new RequestUtil(request).getParameter("byId", "0"));
		
		Businesscategory bcategory=new Businesscategory();
		
		bcategory.setId(id);
		bcategory.setBusinessTypeId(byId);
		bcategory.setName("");

		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		bcs.delBusinesscategory(bcategory);
		
		//删除后重新加载列表
		BusinesstypeService btypes=new BusinesstypeServiceImpl();
		
		Businesstype btype=new Businesstype();
		
		btype.setId(byId);
		
		//根据行业分类查询所有行业次分类
		List list=btypes.getBusinesscategoryByType(btype);
		
		new RequestUtil(request).setAttribute("businesscategoryList", list);
		
		
		return mapping.findForward("list");
	}
	
	
	

}
